#include "radioview.h"
#include <QGridLayout>
#include <QLabel>
#include <QWidget>
#include "ui_radioview.h"
#include "BLL/radioBLl/radiobll.h"
#include "EZTallyArray.h"
#include "EZTallyDictionary.h"
#include "radiomodel.h"
#include "common/asyncimageview.h"
#include "radioviewcell.h"
#include "ui_radioviewcell.h"

RadioView::RadioView() :
    QWidget(0),
    ui(new Ui::RadioView)
{
    ui->setupUi(this);
    _radios = 0;
    RadioBll *bll = new RadioBll();
    connect(bll,SIGNAL(getRadioFinish_SIG(EZTally::EZTallyArray<RadioModel*>*,int)),this,SLOT(getRadioFinish_SLOT(EZTally::EZTallyArray<RadioModel*>*,int)));
    bll->getRadios();

}

RadioView::~RadioView()
{
    if(_radios){
        _radios->Release();
    }
    delete ui;
}
EZTally::EZTallyArray<RadioModel *> *RadioView::radios() const
{
    return _radios;
}

void RadioView::setRadios(EZTally::EZTallyArray<RadioModel *> *radios)
{
    if(radios){
        radios->Retain();
    }
    if(_radios){
        _radios->Release();
    }
    _radios = radios;
    reViewRadios();
}



void RadioView::getRadioFinish_SLOT(EZTally::EZTallyArray<RadioModel*> *radios,int error)
{
    RadioBll *bll = (RadioBll*)sender();
    bll->deleteLater();
    if(error!=0){
        return;
    }
    setRadios(radios);
}

void RadioView::reViewRadios()
{
    QGridLayout *layout = ui->gridLayout;
    for(int i=0;i<_radios->Count();i++){
        RadioModel *radio = _radios->ObjectAtIndex(i);

        RadioViewCell *cell = new RadioViewCell(this);
        cell->setRadio(radio);

        int row = i/3;
        int column = i%3;
        layout->addWidget(cell,row,column,Qt::AlignHCenter|Qt::AlignVCenter);

    }
}
